#include "StompedReality/Core/Graphics/BasicRenderFunctions.h"
#include "StompedReality/Core/Graphics/GLExtensions.h"
#include <GL/gl.h>
#include <iostream>
namespace StompedReality
{

namespace Graphics
{

namespace Functions
{

TranslateRotate::TranslateRotate ( unsigned char renderOrder, bool push, bool isStatic ) :
  StompedReality::Graphics::RenderFunction ( renderOrder, isStatic )
{
  this->PushMatrix = push;
  this->Translation = StompedReality::Math::Vector2 ( 0, 0 );
  this->Depth = 0;
  this->Rotation = 0;
  this->CopyDelegate = 0;
}

TranslateRotate::~TranslateRotate ( )
{

}

void TranslateRotate::Draw ( StompedReality::IGame* game )
{
  if ( this->PushMatrix )
    glPushMatrix ( );

  glTranslatef ( this->Translation.GetX( ), this->Translation.GetY( ), this->Depth );
  glRotatef ( this->Rotation, 0, 0, 1 );
}

void TranslateRotate::CopyRenderData ( )
{
  if ( this->CopyDelegate != 0 )
    this->CopyDelegate ( this );
};

void PopMatrix::Draw ( StompedReality::IGame* game )
{
  glPopMatrix ( );
}

void PopMatrix::CopyRenderData ( )
{

}

RenderPatch::RenderPatch ( unsigned char renderOrder ) :
  StompedReality::Graphics::RenderFunction ( renderOrder, true )
{
  this->Offset = StompedReality::Math::Vector2 ( 0, 0 );
  this->PatchBinding = 0;
  this->TextureBinding = 0;
}

void RenderPatch::Draw ( StompedReality::IGame* game )
{
  glPushMatrix( );
  glTranslatef ( this->Offset.GetX( ), this->Offset.GetY( ), 0 );
  GLExtensions::glBindBufferARB ( GL_ARRAY_BUFFER, this->PatchBinding );
  glVertexPointer ( 2, GL_FLOAT, 16, BUFFER_OFFSET ( 0 ) );
  glTexCoordPointer ( 2, GL_FLOAT, 16, BUFFER_OFFSET ( 8 ) );
  glBindTexture ( GL_TEXTURE_2D, this->TextureBinding );
  glDrawArrays ( GL_TRIANGLE_STRIP, 0, 4 );
  glPopMatrix( );
}

void RenderPatch::CopyRenderData ( )
{
  if ( this->CopyDelegate != 0 )
    this->CopyDelegate ( this );

}

RenderRectangle::RenderRectangle ( unsigned char renderOrder ) :
  StompedReality::Graphics::RenderFunction ( renderOrder, true )
{
  this->Center = StompedReality::Math::Vector2 ( 0, 0 );
  this->HalfSize = StompedReality::Math::Vector2 ( 0, 0 );
  this->CopyDelegate = 0;
}

void RenderRectangle::Draw ( StompedReality::IGame* game )
{
  glPushMatrix( );
  glTranslatef( this->Center.GetX( ), this->Center.GetY( ), 0 );
  glDisable ( GL_TEXTURE_2D );
  glLineWidth ( 2 );
  glColor3f ( 1, 1, 1 );
  glBegin ( GL_LINE_LOOP );
  glVertex2f ( -this->HalfSize.GetX( ), -this->HalfSize.GetY( ) );
  glVertex2f ( -this->HalfSize.GetX( ), this->HalfSize.GetY( ) );
  glVertex2f ( this->HalfSize.GetX( ), this->HalfSize.GetY( ) );
  glVertex2f ( this->HalfSize.GetX( ), -this->HalfSize.GetY( ) );
  glEnd( );
  glColor3f ( 1, 1, 1 );
  glEnable ( GL_TEXTURE_2D );
  glPopMatrix( );
}

void RenderRectangle::CopyRenderData ( )
{
  if ( this->CopyDelegate != 0 )
    this->CopyDelegate ( this );

}

}

}

}